package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.Columns;
import com.example.buildtable.pojo.Newsreport;
import com.example.buildtable.pojo.Product;
import com.example.common.util.R;
import com.example.buildtable.pojo.ProductSubcategory;
import com.example.buildtable.service.ProductSubcategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "产品子分类")
@RequestMapping(value = "/productSubcategory/")
public class ProductSubcategoryController {
    @Autowired
    private ProductSubcategoryService productSubcategoryService;
    
    @Operation(summary = "查询所有产品子分类数据")
    @GetMapping(value = "/getAllProductSubcategory")
    public R getAllProductSubcategory() {
        LambdaJoinQueryWrapper<ProductSubcategory> wrapper = new LambdaJoinQueryWrapper<>(ProductSubcategory.class);
        wrapper.innerJoin(Product.class, Product::getProductId, ProductSubcategory::getProductId);
        List<ProductSubcategory> productSubcategoryList = productSubcategoryService.list(wrapper);
        return R.ok("查询所有产品子分类数据成功").data("productSubcategoryList", productSubcategoryList);
    }
    
    @Operation(summary = "增加产品子分类")
    @PostMapping(value = "/addProductSubcategory")
    public R addProductSubcategory(ProductSubcategory productSubcategory) {
        boolean flag = productSubcategoryService.save(productSubcategory);
        if (flag) {
            return R.ok("增加产品子分类成功");
        } else {
            return R.ok("增加产品子分类失败");
        }
    }

    @Operation(summary = "删除产品子分类")
    @GetMapping(value = "/deleteProductSubcategory/{productSubcategoryId}")
    public R deleteProductSubcategory(@PathVariable("productSubcategoryId") Integer productSubcategoryId) {
        boolean flag = productSubcategoryService.removeById(productSubcategoryId);
        if (flag) {
            return R.ok("删除产品子分类成功");
        } else {
            return R.ok("删除产品子分类失败");
        }
    }

    @Operation(summary = "按照产品子分类查看")
    @GetMapping(value = "/getProductSubcategoryById/{productSubcategoryId}")
    public R getProductSubcategoryById(@PathVariable("productSubcategoryId") Integer productSubcategoryId) {
        LambdaJoinQueryWrapper<ProductSubcategory> wrapper = new LambdaJoinQueryWrapper<>(ProductSubcategory.class);
        wrapper.innerJoin(Product.class, Product::getProductId, ProductSubcategory::getProductId);
        wrapper.eq(ProductSubcategory::getProductSubcategoryId, productSubcategoryId);
        ProductSubcategory productSubcategory = productSubcategoryService.getOne(wrapper);
        if (productSubcategory != null) {
            return R.ok("按照产品子分类查看成功").data("productSubcategory", productSubcategory);
        } else {
            return R.error("按照产品子分类查看失败");
        }
    }

    @Operation(summary = "修改产品子分类")
    @PostMapping(value = "/updateProductSubcategory")
    public R updateProductSubcategory(ProductSubcategory productSubcategory) {
        boolean flag = productSubcategoryService.updateById(productSubcategory);
        if (flag) {
            return R.ok("修改产品子分类成功");
        } else {
            return R.ok("修改产品子分类失败");
        }
    }
}